﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Autodesk.Revit.DB;

namespace YJKInspectModel.ModelValidation.ClearHeightCheck
{
	public class SelectCategrayType
	{
		public SelectCategrayType(List<Element> elementList)
		{
			this.m_elementList = elementList;
		}

		public void SetCategotyList(List<CategoryItem> categoryItemList)
		{
			this.m_CategoryItemList.Clear();
			this.m_CategoryItemList = categoryItemList;
		}

		public void initCategoryItemList()
		{
			foreach (Element element in this.m_elementList)
			{
				if (element != null && element.Category != null && !(null == element.GetType()) && (element.Category.Id.IntegerValue == -2008099 || element.Category.Id.IntegerValue == -2008013 || element.Category.Id.IntegerValue == -2001140 || element.Category.Id.IntegerValue == -2001320 || element.Category.Id.IntegerValue == -2008122 || element.Category.Id.IntegerValue == -2008123 || element.Category.Id.IntegerValue == -2008130 || element.Category.Id.IntegerValue == -2008126 || element.Category.Id.IntegerValue == -2000038 || element.Category.Id.IntegerValue == -2000032 || element.Category.Id.IntegerValue == -2008044 || element.Category.Id.IntegerValue == -2008049 || element.Category.Id.IntegerValue == -2008055 || element.Category.Id.IntegerValue == -2008000 || element.Category.Id.IntegerValue == -2008010 || element.Category.Id.IntegerValue == -2008016))
				{
					Category category = element.Category;
					int num = this.m_CategoryItemList.FindIndex((CategoryItem value) => value.CategoryId() == category.Id.IntegerValue);
					ObjectTypeItem objectTypeItem = new ObjectTypeItem((element.Document.GetElement(element.GetTypeId()) as Autodesk.Revit.DB.ElementType).Name);
					if (-1 == num)
					{
						CategoryItem categoryItem = new CategoryItem(category);
						categoryItem.AddObjectTypeItem(objectTypeItem);
						this.m_CategoryItemList.Add(categoryItem);
					}
					else
					{
						this.m_CategoryItemList[num].AddObjectTypeItem(objectTypeItem);
					}
				}
			}
		}

		public List<CategoryItem> getCategoryList()
		{
			return this.m_CategoryItemList;
		}

		public void SaveToXmlFile(bool isCheckHeight)
		{
			string tempPath = Path.GetTempPath();
			string filename;
			if (!isCheckHeight)
			{
				filename = tempPath + "ClearHeightPlanSelect.xml";
			}
			else
			{
				filename = tempPath + "ClearHeightSelect.xml";
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.RemoveAll();
			XmlNode xmlNode = xmlDocument.CreateElement("CategoryTypeList");
			xmlDocument.AppendChild(xmlNode);
			for (int i = 0; i < this.m_CategoryItemList.Count; i++)
			{
				XmlElement xmlElement = xmlDocument.CreateElement("Category");
				xmlElement.SetAttribute("id", this.m_CategoryItemList[i].CategoryId().ToString());
				xmlElement.SetAttribute("name", this.m_CategoryItemList[i].CategoryName());
				if (this.m_CategoryItemList[i].Selected)
				{
					xmlElement.SetAttribute("Selected", "true");
				}
				else
				{
					xmlElement.SetAttribute("Selected", "false");
				}
				List<ObjectTypeItem> objectTypeItemList = this.m_CategoryItemList[i].ObjectTypeItemList;
				for (int j = 0; j < objectTypeItemList.Count; j++)
				{
					XmlElement xmlElement2 = xmlDocument.CreateElement("objectType");
					xmlElement2.SetAttribute("name", objectTypeItemList[j].getTypeName());
					if (objectTypeItemList[j].Selected)
					{
						xmlElement2.SetAttribute("Selected", "true");
					}
					else
					{
						xmlElement2.SetAttribute("Selected", "false");
					}
					xmlElement.AppendChild(xmlElement2);
				}
				xmlNode.AppendChild(xmlElement);
			}
			xmlDocument.Save(filename);
		}

		public static List<CategoryItem> getCategoryItemListFromXML(bool isCheckHeight)
		{
			List<CategoryItem> result = new List<CategoryItem>();
			string tempPath = Path.GetTempPath();
			string text;
			if (!isCheckHeight)
			{
				text = tempPath + "ClearHeightPlanSelect.xml";
			}
			else
			{
				text = tempPath + "ClearHeightSelect.xml";
			}
			if (!File.Exists(text))
			{
				return result;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("CategoryTypeList");
			if (xmlNodeList.Count == 1)
			{
				result = SelectCategrayType.getCategoryItemData(xmlNodeList[0]);
			}
			return result;
		}

		private static List<CategoryItem> getCategoryItemData(XmlNode node)
		{
			List<CategoryItem> list = new List<CategoryItem>();
			XmlNodeList childNodes = node.ChildNodes;
			for (int i = 0; i < childNodes.Count; i++)
			{
				int id = Convert.ToInt32(childNodes[i].Attributes[0].Value);
				string value = childNodes[i].Attributes[1].Value;
				bool isSelected = childNodes[i].Attributes[2].Value.Equals("true");
				CategoryItem categoryItem = new CategoryItem(id, value, isSelected);
				List<ObjectTypeItem> objectTypeList = new List<ObjectTypeItem>();
				objectTypeList = SelectCategrayType.getObjectTypeItemData(childNodes[i]);
				categoryItem.AddObjectTypeItem(objectTypeList);
				list.Add(categoryItem);
			}
			return list;
		}

		private static List<ObjectTypeItem> getObjectTypeItemData(XmlNode node)
		{
			List<ObjectTypeItem> list = new List<ObjectTypeItem>();
			XmlNodeList childNodes = node.ChildNodes;
			for (int i = 0; i < childNodes.Count; i++)
			{
				string value = childNodes[i].Attributes[0].Value;
				bool isSelected = childNodes[i].Attributes[1].Value.Equals("true");
				ObjectTypeItem item = new ObjectTypeItem(value, isSelected);
				list.Add(item);
			}
			return list;
		}

		public List<CategoryItem> m_CategoryItemList = new List<CategoryItem>();

		private List<Element> m_elementList = new List<Element>();
	}
}
